<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<%@include file="../../commons/meta.jsp"%>
<link rel="stylesheet" href="${ctx}assets/plugins/pure/pure-min.css" media="all" />
<script type="text/javascript" src="${ctx}assets/plugins/plupload/2.2.1/js/plupload.full.min.js"></script>
<script type="text/javascript" src="${ctx}assets/plugins/plupload/2.2.1/js/i18n/zh_CN.js"></script>
<script type="text/javascript">
  function validateForm() {
    if (empty($("#majorCode").val())) {
      alertError('请输入专业代码！');
      return false;
    }
    if(isNaN($("#majorCode").val())){
      alertError('专业代码，请输入数字！');
      return false;
    }
    if (empty($("#majorName").val())) {
      alertError('请输入专业名称！');
      return false;
    }
    if (empty($("#majorSubject").val())) {
      alertError('请选择学科分类！');
      return false;
    }
    if (empty($("#majorClass").val())) {
      alertError('请选择专业分类！');
      return false;
    }
  }
  //  关闭弹出框 
  jQuery(document).ready(function() {
    $("#back_btn").click(function() {
      window.location.href = "${ctx}api/majors/"
    });
  });
</script>
</head>
<body>
    <%@include file="../../commons/head.jsp"%>
    <div class="content">
        <%@include file="../../commons/menu.jsp"%>
        <!--右侧展示部分-->
        <div class="right-zq">
            <!--center content-->
            <div class="dynamic">
                <div class="role dynamiclayer" style="display: block;">
                    <div class="title orange2">
                        <span class="left">专业管理</span> <label> > </label> <span>专业信息管理</span> <label> > </label> <span>添加</span>
                    </div>
                    <form class="" id="forms" method="" action="">
                        <div class="news-table">
                            <table>
                                <tr>
                                    <td class="item">专业代码:<span class="error">*</span></td>
                                    <td width="40%"><input class="p100" id="majorCode" name="majorCode" type="text" placeholder="请输入专业代码" value="" /></td>
                                </tr>
                                <tr>
                                    <td class="item">专业名称:<span class="error">*</span></td>
                                    <td width="40%"><input class="p100" id="majorName" name="majorName" type="text" placeholder="请输入专业名称" value="" /></td>
                                </tr>
                                <tr>
                                    <td class="item">专业层次:<span class="error">*</span></td>
                                    <td width="40%">
                                        <select id="majorLevel" name="majorLevel" class="p100" style="text-align: center;">
                                            <option value="1">本科</option>
                                            <option value="2">专科</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">学年:<span class="error">*</span></td>
                                    <td width="40%">
                                        <select id="learnYear" name="learnYear" class="p100" style="text-align: center;">
                                            <option value="三年">三年</option>
                                            <option value="四年">四年</option>
                                            <option value="五年">五年</option>
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">学科:<span class="error">*</span></td>
                                    <td width="40%">
                                        <select id="majorSubject" name="majorSubject" class="p100" 
                                            style="text-align: center;" onchange="getMajorClass()">
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">专业类:<span class="error">*</span></td>
                                    <td width="40%">
                                    <select id="majorClass" name="majorClass" class="p100" 
                                        style="text-align: center;" >
                                    </select>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">授予学位:</td>
                                    <td width="40%"><input class="p100" id="awardDegree" name="awardDegree" type="text" placeholder="请输入授予学位" value="" /></td>
                                </tr>
                                <tr>
                                    <td class="item">专业简介:</td>
                                    <td width="40%">
                                        <textarea id="majorInfo" rows="5" cols="50"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">培养目标:</td>
                                    <td width="40%">
                                        <textarea id="raiseGoal" rows="5" cols="50"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">培养要求:</td>
                                    <td width="40%">
                                        <textarea id="raiseRequire" rows="5" cols="50"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">主要课程:</td>
                                    <td width="40%">
                                        <textarea id="mainCourse" rows="5" cols="50"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td class="item">知识能力:</td>
                                    <td width="40%">
                                        <textarea id="knowledgeCapacity" rows="5" cols="50"></textarea>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td>
                                        <button class="orange2" type="button" id="add_btn" onclick="add()">保存&nbsp;<i class="fa fa-floppy-o"></i>
                                        </button> &nbsp;&nbsp;
                                        <button class="orange2" type="button" id="back_btn">返回&nbsp;<i class="fa fa-undo"></i>
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </form>
                </div>
            </div>
            <!--center content over-->
        </div>
    </div>
    <%@include file="../../commons/footpop.jsp"%>
    <!-- 页尾，提示消息 -->
    <jsp:include page="../../commons/foot_message.jsp"></jsp:include>
<script type="text/javascript">
// 初始化时加载学科下拉框数据
getMajorSubject();
/**
 * 获取学科
 * @returns
 */
function getMajorSubject() {
  // 默认按id排序
  var query = {};
  // 学科的父级代码默认为"00"
  query.parentCode = "00";
  var searchJsonUrl = 'api/major-types/findAll';
  var param = JSON.stringify(query);

  $.ajax({
   url: searchJsonUrl,
   data: param,
   type: 'POST',
   contentType: "application/json",
   dataType: 'json',
   success: function (result) {
     debugs(result);
     var list = result.list;
     if (list.length > 0) {
       var option = "<option value=''>请选择学科</option>";
       $.each(list, function(index, item) {
         option += "<option code='" + item.majorTypeCode + "' value='" + item.majorTypeName + "'>" + item.majorTypeName + "</option>";
       });
       $("#majorSubject").html(option);
     }
   }, 
   error: function (xhr) {
     alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
   }
  });
}

/**
 * 获取专业类
 * @returns
 */
function getMajorClass() {
  var parentCode = $('#majorSubject option:selected').attr("code");
  debugs(parentCode);
  
  // 默认按id排序
  var query = {};
  query.parentCode = parentCode;
  var searchJsonUrl = 'api/major-types/findAll';
  var param = JSON.stringify(query);

  $.ajax({
   url: searchJsonUrl,
   data: param,
   type: 'POST',
   contentType: "application/json",
   dataType: 'json',
   success: function (result) {
     debugs(result);
     var list = result.list;
     if (list.length > 0) {
       var option = "";
       $.each(list, function(index, item) {
         option += "<option code='" + item.majorTypeCode + "' value='" + item.majorTypeName + "'>" + item.majorTypeName + "</option>";
       });
       $("#majorClass").html(option);
     }
   }, 
   error: function (xhr) {
     alertError("系统异常，请联系管理员！");// 请求失败遇到异常触发
   }
  });
}

// 添加
function add() {
  // 先进行校验
  validateForm();

  // 将提交按钮置灰
  $('#add_btn').attr("disabled", true);

  var obj = {};
  obj.majorCode = $('#majorCode').val();
  obj.majorName = $('#majorName').val();
  obj.majorLevel = parseInt($('#majorLevel').val());
  obj.majorSubject = $('#majorSubject').val();
  obj.majorClass = $('#majorClass').val();
  obj.learnYear = $('#learnYear').val();
  obj.awardDegree = $('#awardDegree').val();
  obj.majorInfo = $('#majorInfo').val();
  obj.raiseRequire = $('#raiseRequire').val();
  obj.raiseGoal = $('#raiseGoal').val();
  obj.mainCourse = $('#mainCourse').val();
  obj.knowledgeCapacity = $('#knowledgeCapacity').val();

  $.ajax({
    url: '${ctx}api/majors/addMajor',
    data: JSON.stringify(obj),
    type: 'POST',
    contentType: "application/json",
    dataType: 'json',
    success: function(data) {
      if (data.flag) {
        alertOk("添加成功！");
        setTimeout("time()", 1000);
        reset();
      } else {
        alertError("添加失败！");
      }
      $('#add_btn').attr("disabled", false);
    },
    error: function() {
      alertError("系统异常，请联系管理员！");
    }
  });
}

function reset() {
  $('#majorCode').val("");
  $('#majorName').val("");
  $('#awardDegree').val("");
  $('#majorInfo').val("");
  $('#raiseRequire').val("");
  $('#raiseGoal').val("");
  $('#mainCourse').val("");
  $('#knowledgeCapacity').val("");
}
</script>
</body>
</html>
